#!/usr/bin/env bash

# https://mirrors.lzu.edu.cn/gnu/readline
# https://mirrors.lzu.edu.cn/gnu/readline/readline-8.3.tar.gz

# https://ftp.gnu.org/gnu/ncurses/
# https://ftp.gnu.org/gnu/ncurses/ncurses-6.4.tar.gz

# https://github.com/hanslub42/rlwrap
# https://github.com/hanslub42/rlwrap/releases/download/v0.47.1/rlwrap-0.47.1.tar.gz

set -evx
export OHOS_ROOT_DIR=~/openharmony/master/
export OHOS_ROOT_RK_OUT_DIR=$OHOS_ROOT_DIR/out/rk3568
export PATH=$OHOS_ROOT_DIR/prebuilts/clang/ohos/linux-x86_64/llvm/bin:$PATH
export CC=clang
export CXX=clang++
export CFLAGS=$(cat<<EOF
-DV8_DEPRECATION_WARNINGS
-D_GNU_SOURCE
-DHAVE_SYS_UIO_H
-D__MUSL__
-D_LIBCPP_HAS_MUSL_LIBC
-D__BUILD_LINUX_WITH_CLANG
-D__STDC_CONSTANT_MACROS
-D__STDC_FORMAT_MACROS
-DCOMPONENT_BUILD
-D__GNU_SOURCE=1
-DCHROMIUM_CXX_TWEAK_INLINES
-DNDEBUG
-DNVALGRIND
-DDYNAMIC_ANNOTATIONS_ENABLED=0
-DL2_STANDARD
-DOHOS_PLATFORM
-I$OHOS_ROOT_RK_OUT_DIR/obj/third_party/musl/usr/include/arm-linux-ohos
-I$OHOS_ROOT_RK_OUT_DIR/override/third_party
-I$OHOS_ROOT_RK_OUT_DIR/../..
-I$OHOS_ROOT_RK_OUT_DIR/gen
-fno-strict-aliasing
-Wno-builtin-macro-redefined
-D__DATE__=
-D__TIME__=
-D__TIMESTAMP__=
-funwind-tables
-fPIC
-fcolor-diagnostics
-fmerge-all-constants
-Xclang
-mllvm
-Xclang
-instcombine-lower-dbg-declare=0
-no-canonical-prefixes
-flto=thin
-fsplit-lto-unit
-ffunction-sections
-fno-short-enums
--target=arm-linux-ohos
-march=armv7-a
-mfloat-abi=softfp
-mtune=generic-armv7-a
-fstack-protector-strong
-mfpu=neon
-mthumb
-O2
-fno-ident
-fdata-sections
-ffunction-sections
-fomit-frame-pointer
-gdwarf-3
-g2
-ggnu-pubnames
-fno-common
-Wheader-hygiene
-Wstring-conversion
-Wtautological-overlap-compare
-fvisibility=default
-ftrivial-auto-var-init=zero
-enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
-DBINDER_IPC_32BIT
-pipe
-DCONFIG_STANDARD_SYSTEM
-DBUILD_PUBLIC_VERSION
--sysroot=$OHOS_ROOT_RK_OUT_DIR/obj/third_party/musl
EOF
)

export CFLAGS=$(echo $CFLAGS | tr '\n' ' ')
echo $CFLAGS
export CXXFLAGS=-std=c++17

rm -rf ncurses-6.4
tar -xf ncurses-6.4.tar.gz
pushd ncurses-6.4
    grep -n cross_compiling configure
    sed -i 's#^cross_compiling=no$#cross_compiling=yes#' configure
    grep -n cross_compiling configure
    ./configure
    make libs -j
popd


echo $CFLAGS

export CFLAGS="$CFLAGS -I$(pwd)/ncurses-6.4/include -L$(pwd)/ncurses-6.4/lib"

echo $CFLAGS


rm -rf readline
tar -xf readline-8.3.tar.gz
mv readline-8.3 readline
pushd readline
    grep -n cross_compiling configure
    sed -i 's#^cross_compiling=no$#cross_compiling=yes#' configure
    grep -n cross_compiling configure
    ./configure
    make libreadline.a -j
popd


echo $CFLAGS

export CFLAGS="$CFLAGS -isystem$(pwd) -L$(pwd)/readline"
export CPPFLAGS="$CFLAGS" # rlwrap configure 时校验 readline 功能用这个环境变量了

echo $CFLAGS


rm -rf rlwrap-0.47.1
tar -xf rlwrap-0.47.1.tar.gz
pushd rlwrap-0.47.1
    grep -n cross_compiling configure
    sed -i 's#^cross_compiling=no$#cross_compiling=yes#' configure
    grep -n cross_compiling configure

    # 不改这行的话在设备上运行会报错退出，把 fatal 改成 warning 后打印告警会接着跑，实测是能用的。
    sed -i 's#myerror(FATAL|NOERRNO, "My terminal#myerror(WARNING|NOERRNO, "My terminal#' src/term.c

    ./configure --without-libptytty
    make
popd

llvm-strip rlwrap-0.47.1/src/rlwrap